package com.ruoyi.system.controller;

import com.ruoyi.system.domain.ChatMessagesDTO;
import com.ruoyi.system.service.IChatMessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chatMessages")
public class ChatMessagesController {

    @Autowired
    private IChatMessagesService iChatMessagesService;

    @GetMapping("/getChatMessages")
    public AjaxResult getChatMessages(@RequestParam String chatId) {
        if (StringUtils.isEmpty(chatId)) {
            return AjaxResult.error("对话ID不能为空");
        }
        List<ChatMessagesDTO> messages = iChatMessagesService.getChatMessages(chatId);
        return AjaxResult.success(messages);
    }

    @PostMapping("/addChatMessages")
    public AjaxResult addChatMessages(@RequestBody Map<String, String> requestBody) {
        String chatId = requestBody.get("chatId");
        String messageContent = requestBody.get("messageContent");

        if (StringUtils.isEmpty(chatId)) {
            return AjaxResult.error("对话ID不能为空");
        }
        if (StringUtils.isEmpty(messageContent)) {
            return AjaxResult.error("消息内容不能为空");
        }

        try {
            ChatMessagesDTO result = iChatMessagesService.addChatMessage(chatId, messageContent);
            return AjaxResult.success("操作成功", result);
        } catch (Exception e) {
            return AjaxResult.error("新增失败: " + e.getMessage());
        }
    }
}
